package com.ywd.materials.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywd.materials.admin.dto.AdminRoleMenuDto;
import com.ywd.materials.admin.entity.AdminRoleMenuEntity;
import com.ywd.materials.admin.mapper.AdminRoleMenuMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * YWD
 * 2025/11/12
 */
@AllArgsConstructor
@Service
public class AdminRoleMenuServiceImpl extends ServiceImpl<AdminRoleMenuMapper, AdminRoleMenuEntity> {


    public List<AdminRoleMenuEntity> getByRoleId(Long roleId) {
        LambdaQueryWrapper<AdminRoleMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleMenuEntity::getRoleId, roleId);
        List<AdminRoleMenuEntity> list = list(queryWrapper);
        if(CollectionUtil.isEmpty(list)) return null;
        return list;
    }

    public List<AdminRoleMenuEntity> getByMenuId(Long menuId) {
        LambdaQueryWrapper<AdminRoleMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleMenuEntity::getMenuId, menuId);
        List<AdminRoleMenuEntity> list = list(queryWrapper);
        if(CollectionUtil.isEmpty(list)) return null;
        return list;
    }

    public Boolean addRoleMenu(AdminRoleMenuDto dto) {
        List<AdminRoleMenuEntity> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dto.getMenuIds())) {
            dto.getMenuIds().forEach(menuId -> {
                AdminRoleMenuEntity roleMenuEntity = new AdminRoleMenuEntity();
                roleMenuEntity.setMenuId(menuId);
                roleMenuEntity.setRoleId(dto.getId());
                list.add(roleMenuEntity);
            });
        }
        return saveOrUpdateBatch(list);
    }


    public Boolean removeByMenuId(Long menuId) {
        LambdaQueryWrapper<AdminRoleMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleMenuEntity::getMenuId, menuId);
        return remove(queryWrapper);
    }

    public Boolean removeByRoleId(Long roleId) {
        LambdaQueryWrapper<AdminRoleMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleMenuEntity::getRoleId, roleId);
        return remove(queryWrapper);
    }


}
